package cn.bdqn.mallmgr.controller.rec;

import cn.bdqn.mallmgr.pojo.Page;
import cn.bdqn.mallmgr.pojo.Product;
import cn.bdqn.mallmgr.service.ProductService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author
 * @create 2022-07-13 10:42
 */
@RestController
public class ProductController {

    @Resource
    private ProductService productService;

    /**
     * 查询商品
     *
     * @param pageIndex   index
     * @param pageSize    size
     * @param categoryId  类别id
     * @param productName 查询key
     * @param sort_key    排序字段，按时间createTime，按销量dealCount，按价格price
     * @param sort_type   排序规则，0升序1降序
     * @return java.lang.String
     */
    @GetMapping("/productList")
    public String productList(@RequestParam(defaultValue = "1", required = false) Integer pageIndex,
                              @RequestParam(defaultValue = "15", required = false) Integer pageSize,
                              Integer categoryId,
                              String productName,
                              String sort_key,
                              Integer sort_type) {
        Page page = productService.getPageProducts(categoryId, productName, sort_key, sort_type, pageIndex, pageSize);
        return JSONObject.toJSONString(page);
    }

    /**
     * 商品详情
     *
     * @param productId 商品id
     * @return
     */
    @GetMapping("/productDetails")
    public String productDetails(@RequestParam Integer productId) {
        Product product = productService.getProductById(productId);
        return JSONObject.toJSONStringWithDateFormat(product, "yyyy-MM-dd");
    }
}
